package com.example.weatherforecast.city_manager;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.weatherforecast.MainActivity;
import com.example.weatherforecast.R;
import com.example.weatherforecast.database.DatabaseBean;
import com.example.weatherforecast.database.MyDataBaseManager;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class CityManagerActivity extends AppCompatActivity implements View.OnClickListener {

    String Tag = "CityManagerActivity";
    Context context = this;

    // 控件
    ImageButton addIb;  // 添加按钮
    ImageView backIv, deleteIv; //返回，删除
    RecyclerView cityRv;
    // RecyclerView适配器
    CityManagerAdapter adapter;

    // 城市管理数据源
    List<DatabaseBean> list;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_city_manager);

        init();

    }

    private void init() {
        //初始化控件
        addIb = findViewById(R.id.cityIbAdd);
        backIv = findViewById(R.id.cityIvBack);
        deleteIv = findViewById(R.id.cityIvDelete);
        cityRv = findViewById(R.id.cityRv);

        //初始化数据源
        list = new ArrayList<>();

        //添加点击事件
        addIb.setOnClickListener(this);
        backIv.setOnClickListener(this);
        deleteIv.setOnClickListener(this);

        // 给RecyclerView设置布局并添加适配器
        cityRv.setLayoutManager(new LinearLayoutManager(this));
        adapter = new CityManagerAdapter(this, list);
        cityRv.setAdapter(adapter);

        // 重写适配器里的点击事件方法，跳转对应页查看天气
        adapter.setItemClickListener(new CityManagerAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                Log.i(Tag, "单击item-" + position);
                Intent intent = new Intent();
                intent.setClass(context, MainActivity.class);
                intent.putExtra("page", position);
                startActivity(intent);
            }
        });
        // 长按任意一个item进入删除界面
        adapter.setLongClickListener(new CityManagerAdapter.OnLongClickListener() {
            @Override
            public boolean onLongClick(int position) {
                Log.i(Tag, "长按item-" + position);
                Intent intent = new Intent();
                intent.setClass(context, DeleteCityActivity.class);
                startActivity(intent);
                return true;
            }
        });


    }

    /**
     * 获取焦点后，获取数据库中实时的数据，并更新显示
     */
    @Override
    protected void onResume() {
        super.onResume();
        //清空原来的数据
        list.clear();
        // 添加新数据
        list.addAll(MyDataBaseManager.queryAllInfo());
        // 逆序
        Collections.reverse(list);
        // 通知适配器数据可能已改变
        adapter.notifyDataSetChanged();
    }

    @Override
    public void onClick(View v) {
        Intent intent;
        switch (v.getId()) {
            case R.id.cityIbAdd: // 跳转查找城市界面
//                Toast.makeText(this, "点击添加按钮", Toast.LENGTH_SHORT).show();
                // 最多只能同时保存8个城市信息
                if (MyDataBaseManager.getCityCount() <= 7) {
                    intent = new Intent(this, SearchCityActivity.class);
                    startActivity(intent);
                } else {
                    Toast.makeText(this, "存储城市数量已达上限，请删除后再增加", Toast.LENGTH_SHORT).show();
                }

                break;
            case R.id.cityIvBack:
//                Toast.makeText(this, "点击返回按钮", Toast.LENGTH_SHORT).show();
                finish();
                break;
            case R.id.cityIvDelete: // 跳转到删除城市界面
//                Toast.makeText(this, "点击删除按钮", Toast.LENGTH_SHORT).show();
                intent = new Intent(this, DeleteCityActivity.class);
                startActivity(intent);
                break;
        }
    }
}
